﻿/*
 * Cratos Desktop
 * Cratos WPF Helper class
 * 
 * Copyright (c) 2011 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Text;
using System.Linq;
using System.Windows;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Collections.Generic;

namespace Cratos.Desktop
{
	/// <summary>
	/// Cratos WPF Helper class
	/// </summary>
	public static class CratosWPFHelper
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		#endregion

		#region Class Variable declarations
		private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
		#endregion

		#region Class Constructors & Destructors
		#endregion

		#region Class Properties
		#endregion

		#region Class Methods
		/// <summary>
		/// Processes all UI messages currently in the message queue.
		/// </summary>
		public static void DoEvents()
		{
			// Declare the required variables
			DispatcherFrame nestedFrame;
			DispatcherOperation exitOperation;

			// Create new nested message pump.
			nestedFrame = new DispatcherFrame();

			// Dispatch a callback to the current message queue, when getting called,
			// this callback will end the nested message loop.
			// note that the priority of this callback should be lower than that of UI event messages.
			exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);

			// pump the nested message loop, the nested message loop will immediately
			// process the messages left inside the message queue.
			Dispatcher.PushFrame(nestedFrame);

			// If the "exitFrame" callback is not finished, abort it.
			if (exitOperation.Status != DispatcherOperationStatus.Completed)
			{
				exitOperation.Abort();
			}

			// Return to calling method
			return;
		}

		/// <summary>
		/// Retrieve the Treeview Parent item
		/// </summary>
		/// <param name="currentItem"></param>
		/// <returns></returns>
		public static TreeViewItem GetTreeViewParent(TreeViewItem currentItem)
		{
			// Declare the required variables
			DependencyObject returnValue;

			// Retrieve the parent
			returnValue = VisualTreeHelper.GetParent(currentItem);
			while (returnValue != null && !(returnValue is TreeViewItem))
			{
				returnValue = VisualTreeHelper.GetParent(returnValue);
			}

			// Return to calling method
			return (returnValue as TreeViewItem);
		}
		#endregion

		#region Class Support Methods
		private static Object ExitFrame(Object state)
		{
			// Declare the required variables
			DispatcherFrame frame;

			// Create the required Frame
			frame = state as DispatcherFrame;

			// Exit the nested message loop.
			frame.Continue = false;

			// Return to calling method
			return (null);
		}
		#endregion
	}
}
